package com.gjy.sentinel.core.sentinel;

import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.gjy.sentinel.core.util.JsonUtil;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 负责把磁盘（或配置中心）里的 JSON 规则读取出来，
 * 并通过相应的 *RuleManager* 刷新到 Sentinel 内存中。
 * <p>
 * 为了演示，这里使用 **文件系统**（sentinel-rules/xxx.json）；
 * 实际项目可改写成 NacosDataSource 等。
 *
 * @author gjy
 * @version 1.0
 * @since 2025-10-14 17:15:22
 */
public class RuleLoader {

    private static final String RULE_DIR = "D:\\code\\java\\microservice_spc\\utils-all\\sentinel-util\\src\\main\\resources\\sentinel-rules";

    /**
     * 统一入口：一次性加载全部规则
     */
    public static void loadAll() throws IOException {
        loadFlowRules();
        loadDegradeRules();
        loadSystemRules();
        loadParamRules();
        loadAuthorityRules();
    }

    private static void loadFlowRules() throws IOException {
        List<FlowRule> list = read(RULE_DIR + "/flow.json", FlowRule.class);
        if (list != null) {
            FlowRuleManager.loadRules(list);
        }
    }

    private static void loadDegradeRules() throws IOException {
        List<DegradeRule> list = read(RULE_DIR + "/degrade.json", DegradeRule.class);
        if (list != null) {
            DegradeRuleManager.loadRules(list);
        }
    }

    private static void loadSystemRules() throws IOException {
        List<SystemRule> list = read(RULE_DIR + "/system.json", SystemRule.class);
        if (list != null) {
            SystemRuleManager.loadRules(list);
        }
    }

    private static void loadParamRules() throws IOException {
        List<ParamFlowRule> list = read(RULE_DIR + "/param.json", ParamFlowRule.class);
        if (list != null) {
            ParamFlowRuleManager.loadRules(list);
        }
    }

    private static void loadAuthorityRules() throws IOException {
        List<AuthorityRule> list = read(RULE_DIR + "/authority.json", AuthorityRule.class);
        if (list != null) {
            AuthorityRuleManager.loadRules(list);
        }
    }

    /**
     * 读取 JSON 并反序列化成 List<T>，若文件不存在返回 null
     */
    private static <T> List<T> read(String filePath, Class<T> elementClz) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.println("[RuleLoader] file not found: " + filePath);
            return null;
        }
        return JsonUtil.readJsonList(file, elementClz);
    }

}
